package com.lm.upms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lm.common.core.constant.CommonConstants;
import com.lm.common.core.util.R;
import com.lm.common.log.annotation.SysLog;
import com.lm.common.log.enums.BusinessType;
import com.lm.common.security.util.SecurityUtils;
import com.lm.upms.api.dto.MenuTree;
import com.lm.upms.api.entity.SysMenu;
import com.lm.upms.api.vo.MenuVO;
import com.lm.upms.api.vo.TreeUtil;
import com.lm.upms.service.SysMenuService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 菜单信息
 *
 * @author lm
 * @date 2019/7/2 14:34
 */
@Api(tags = "菜单信息")
@Slf4j
@RestController
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查当前用户的树菜单
     *
     * @return
     */
    @GetMapping(value = "/list")
    public R getUserMenu() {
        return R.success(sysMenuService.list(Wrappers.emptyWrapper()));
    }

    /**
     * 查当前用户的树菜单
     *
     * @return
     */
    @GetMapping(value = "/getUserMenuTree")
    public R getUserMenuTree() {
        log.debug("~~~~getUserMenuTree()~~~1: {}, 2:{}", SecurityUtils.getUser(), SecurityUtils.getRoles());
        // 获取符合条件的菜单
        Set<MenuVO> menuSet = new HashSet<>();
        SecurityUtils.getRoles().forEach(roleId -> menuSet.addAll(sysMenuService.findMenuByRoleId(roleId)));

        List<MenuTree> menuTreeList = menuSet.stream()
                .filter(menuVo -> CommonConstants.MENU.equals(menuVo.getMenuType()))
                .map(MenuTree::new)
                .sorted(Comparator.comparingInt(MenuTree::getSort))
                .collect(Collectors.toList());
        return R.success(TreeUtil.bulid(menuTreeList, 0));
    }

    /**
     * 查树菜单
     *
     * @return
     */
    @GetMapping(value = "/tree")
    public R getTree() {
        return R.success(TreeUtil.bulidTree(sysMenuService.list(Wrappers.emptyWrapper()), 0));
    }

    /**
     * 查角色的树菜单
     *
     * @param roleId 角色ID
     * @return
     */
    @GetMapping("/tree/{roleId}")
    public List getRoleTree(@PathVariable Integer roleId) {
        return sysMenuService.findMenuByRoleId(roleId)
                .stream()
                .map(MenuVO::getMenuId)
                .collect(Collectors.toList());
    }

    /**
     * 根据角色编号查询菜单编号（用于勾选）
     *
     * @param roleId
     * @return
     */
    @GetMapping("role/{roleId}")
    public Set<Integer> role(@PathVariable("roleId") Integer roleId) {
        if (null == roleId || roleId <= 0) {
            return null;
        }
        return sysMenuService.selectMenuIdsByRoleId(roleId);
    }

    /**
     * 通过ID查询菜单的详细信息
     *
     * @param id 菜单ID
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        return R.success(sysMenuService.getById(id));
    }

    /**
     * 新增菜单
     *
     * @param sysMenu
     * @return
     */
    @SysLog(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping("save")
    public R save(@Valid @RequestBody SysMenu sysMenu) {
        return R.success(sysMenuService.save(sysMenu));
    }

    /**
     * 更新菜单
     *
     * @param sysMenu
     * @return
     */
    @SysLog(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PostMapping("update")
    public R update(@Valid @RequestBody SysMenu sysMenu) {
        return R.success(sysMenuService.updateMenuById(sysMenu));
    }

    /**
     * 删除菜单
     *
     * @param ids
     * @return
     */
    @SysLog(title = "菜单管理", businessType = BusinessType.DELETE)
    @PostMapping("remove")
    public R removeById(Integer ids) {
        return sysMenuService.removeMenuById(ids);
    }

}
